package com.springboot.java.design.search;

import java.util.Arrays;
import java.util.HashMap;

/**
 * @Author: TongRui乀
 * @Date: 2020-09-01 19:36
 * @description： 给定一个整数数组和一个整数（target）  返回 两个整数和等于target的索引
 */
public class ReturnSum {

    public static void main(String[] args) {

        int[] nums = {1, 2, 3, 4, 5, 6, 7, 9, 10};

        int[] sumNums = getSumNums(nums, 10);

        System.out.println(Arrays.toString(sumNums));

    }

    private static int[] getSumNums(int[] nums, int target){

        HashMap<Integer, Integer> map = new HashMap<>();

        for (int i = 0; i < nums.length; i++) {

            if(map.containsKey(nums[i])){
                return new int[]{map.get(nums[i]), i};
            }else {
                // num[i] + x = target
                map.put(target - nums[i], i);
            }
        }
        return new int[]{0,0};
    }

}
